// index
/**
 * 引用工具
 */
import {
    Ble
} from "../../utils/ble"
import {
    Protocol
} from "../../utils/protocol"

//引用app.js的全局函数声明
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        // tabs
        tabs: [],
        activeTab: 0,
        // scan
        result: "等待扫描",
        // ble
        bleStatus: "",
        macAddress: 0x00FFEECC,
        // room&dev
        areaRoom: [{
            idRoom: 1,
            nameRoom: '客厅',
            dev: [{
                // 0-2 means 3types led
                devType: 1,
                devName: '客厅的灯'
            }]
        }, {
            idRoom: 2,
            nameRoom: '卧室',
            dev: [{
                // 0-2 means 3types led
                devType: 1,
                devName: '卧室的灯'
            }]
        }],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //蓝牙初始化并提供蓝牙状态标志
        Ble.initBle({
            success: (res) => {
                console.log(JSON.stringify(res) + "*****")
                this.setData({
                    bleStatus: "初始化蓝牙成功"
                })
            },
            fail: (error) => {
                console.log(JSON.stringify(error) + "----")
                this.setData({
                    bleStatus: "请重启蓝牙后，重新启动小程序"
                })
            }
        })
        console.log("onLoad")

        // tabs
        const titles = ['首页', '客厅']
        const tabs = titles.map(item => ({
            title: item
        }))
        this.setData({
            tabs
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    /**
     * 自定义页面处理函数
     */

    // 显示侧边栏按钮        
    showDrawer() {
        this.setData({
            leftDrawer: true,
        })
    },

    // 关闭侧边栏按钮 
    closeDrawer(e) {
        this.setData({
            leftDrawer: false
        })
    },


    //侧边栏跳转 打开对应页面
    gotoScan() {
        wx.navigateTo({
            url: '/pages/index/slider/scan/scan',
        })
    },
    gotoQrcode() {
        wx.navigateTo({
            url: '/pages/index/slider/qrcode/qrcode',
        })
    },
    gotoDecoding() {
        wx.navigateTo({
            url: '/pages/index/slider/decoding/decoding',
        })
    },
    gotoReset() {
        wx.navigateTo({
            url: '/pages/index/slider/reset/reset',
        })
    },
    gotoManagement() {
        wx.navigateTo({
            url: '/pages/index/slider/management/management',
        })
    },
    gotoInstruction() {
        wx.navigateTo({
            url: '/pages/index/slider/instruction/instruction',
        })
    },
    gotoAbout() {
        wx.navigateTo({
            url: '/pages/index/slider/about/about',
        })
    },

    /* 蓝牙相关函数 */
    // 蓝牙数据广播
    sendData(bytes) {
        Ble.server.startAdvertising({
            advertiseRequest: {
                serviceUuids: bytes
            },
            powerLevel: 'high',
            success: (res) => {
                console.log(" advertise message success ")
                this.timer = setTimeout(() => {
                    Ble.server.stopAdvertising({})
                }, 300)
            },
            fail: (error) => {
                console.log(JSON.stringify(error))
            },
            complete: () => {
                console.log(" advertise message complete ")
            }
        })
    },
    // 蓝牙配对（对码）
    pairDevice(e) {
        let params = new Uint8Array(9)
        params[0] = 0x01
        params[3] = 0xFF
        params[4] = 0xEE
        params[5] = 0xCC
        let bytes = Protocol.encode(0x02, this.data.macAddress, params)
        this.sendData(bytes)
    },
    // 蓝牙开灯
    turnOn(e) {

        let params = new Uint8Array(9)
        params[0] = 0x23
        let bytes = Protocol.encode(0x3B, this.data.macAddress, params)
        this.sendData(bytes)
    },
    // 蓝牙关灯
    turnOff(e) {
        let params = new Uint8Array(9)
        params[0] = 0x24
        let bytes = Protocol.encode(0x3B, this.data.macAddress, params)
        this.sendData(bytes)
    },

    //二维码扫描
    scanCode() {
        var myThis = this;
        wx.scanCode({
            success(res) {
                switch (res.result) {
                    case "11111":
                        myThis.setData({
                            src: "https://qcloudbbs-10019892.cos.ap-shanghai.myqcloud.com/dog.jpg"
                        })
                        break;
                    case "22222":
                        myThis.setData({
                            src: "https://qcloudbbs-10019892.cos.ap-shanghai.myqcloud.com/cat.jpg"
                        })
                        break;
                    case "33333":
                        myThis.setData({
                            src: "https://qcloudbbs-10019892.cos.ap-shanghai.myqcloud.com/bird.jpg"
                        })
                        break;
                    default:
                        myThis.setData({
                            src: "https://qcloudbbs-10019892.cos.ap-shanghai.myqcloud.com/oops.jpg"
                        })
                }
                //   myThis.setData({
                //     result: res.result,
                //     scanType: res.scanType
                //   })
                // console.log(res.result)   
            }
        })
    },


    // tabs funtions
    onTabCLick(e) {
        const index = e.detail.index
        this.setData({
            activeTab: index
        })
    },

    onChange(e) {
        const index = e.detail.index
        this.setData({
            activeTab: index
        })
    }

})